package com.bytedance.scenedemo.activity_compatibility.softkeyboard;

import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.navigation.EmptyAbility;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.activity_compatibility.configuration.ConfigurationDemoScene;
import com.bytedance.scenedemo.utility.ColorUtil;
import com.bytedance.scenedemo.view.ClassPathInfoTextView;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;

/**
 * 输入法问题
 * ohos framework has three soft input policies: SOFT_INPUT_ADJUST_RESIZE, SOFT_INPUT_ADJUST_PAN, SOFT_INPUT_ADJUST_NOTHING
 * Scene container Activity's ohosManifest.xml ohos:windowSoftInputMode must be set to **adjustPan** or **adjustResize** or **adjustNothing**,
 * adjustUnspecified flag has bug will cause `requireActivity().getWindow().setSoftInputMode` not working, then system will
 * pick the best one depending on the contents of the window
 * If you push a AppCompatScene, everything will work fine, if you push a GroupScene, you should set root view's ohos:fitsSystemWindows to true,
 * otherwise SOFT_INPUT_ADJUST_RESIZE will have problem
 *
 * @author JiangQi
 * @since 2018-08-19
 */
public class SoftKeyboardDemoScene extends GroupScene {

    private ComponentContainer parse;

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        parse = (ComponentContainer) inflater.parse(ResourceTable.Layout_basic_layout, container, false);
        return parse;
    }

    @Override
    public void onActivityCreated(Intent intent) {
        super.onActivityCreated(intent);
        this.getView().setBackground(ColorUtil.getMaterialColor(0));
        ClassPathInfoTextView classPathInfoTextView = (ClassPathInfoTextView) parse.findComponentById(ResourceTable.Id_class_path_info);
        classPathInfoTextView.setText(this.getClass().getName());
        Text name = (Text) parse.findComponentById(ResourceTable.Id_name);
        name.setText(ResourceTable.String_SoftKeyboard);
        Button btn = (Button) parse.findComponentById(ResourceTable.Id_btn);
        btn.setText("使用 AdjustResize 模式打开新页面");
        btn.setClickedListener(component -> {
            SceneExtensions.getNavigationScene(this).push(SoftKeyboardResizeScene.class);
        });

    }

    public static final void focusAndShowInputMethod(TextField textField) {
        Companion.focusAndShowInputMethod(textField);
    }

    public static final class Companion {
        private static int select;

        //        @JvmStatic
        public static void focusAndShowInputMethod(TextField textField) {
            if (textField == null) {
                return;
            }
            textField.requestFocus();
            textField.setText(textField.getText());
            if (textField.getText() == null) {
                select = 0;
            } else {
                select = textField.getText().length();
            }
//            textField.setSelectionColor();
//            InputMethodManager inputMethodManager = textField.getContext().getSystemService("input_method");
//            inputMethodManager.showSoftInput((Component) textField, 0);
        }

        private Companion() {
        }

    }


}


